package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.db.domain.Members;
import com.mall.db.service.MembersService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/members/mcs")
public class MembersController extends BaseController<Members>{

    @Resource(name="membersService")
    private MembersService msc;

    @Override
    public Map<String, Object> find(Members members) {
        //实现分页
        PageHelper.startPage(members.getPage(),members.getLimit());

        QueryWrapper<Members> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(members.getId())){
            qw.eq("id",members.getId());
        }if (!StringUtils.isEmpty(members.getUsername())){
            qw.eq("username",members.getUsername());
        }if (!StringUtils.isEmpty(members.getPhone())){
            qw.eq("phone",members.getPhone());
        }
        List<Members> list = msc.list(qw);

        PageInfo<Members> page = new PageInfo<>(list,members.getLimit());

        return this.findMap(page.getList(), page.getTotal());
    }

    @Override
    public Map<String, Object> add(Members members) {
        //TODO 密码加密
        boolean b = msc.save(members);
        return this.iud("新增",b,members);
    }

    @Override
    public Map<String, Object> update(Members members) {
        boolean b = msc.updateById(members);
        return this.iud("编辑",b,members);
    }
    @Override
    public Map<String, Object> delete(Members members) {
        boolean b = msc.removeById(members);
        return this.iud("删除",b,members);
    }
}
